import { Component, OnInit } from '@angular/core';
import { ContractService } from "./contract-service.service";
import { DataService } from '../../../../services/data.service';
import { PaginationModel } from 'app/widgets/pagination';
import * as flatpickr from 'flatpickr';
import { zh } from "flatpickr/dist/l10n/zh.js";
import * as _ from 'lodash';

@Component({
  selector: 'app-contract-service',
  templateUrl: './contract-service.component.html',
  styleUrls: ['./contract-service.component.scss'],
  providers: [ContractService]
})
export class ContractServiceComponent implements OnInit {
  //审核信息
  public sAuditInfo: any;
  // 字典值
  public dictionaryList: any = {};
  // 产品类型
  public sFundType: any = [];
  // 合同信息
  public contractBody: any = {};
  // 服务有效条目
  public effectiveBody: any = {};
  // 条目变更记录数据
  public queryList: any;
  // 条目变更记录查询post数据
  public queryPost: any = {
    pageIndex: 1,
    pageSize: 10,
    nFundNo: this.contractBody.nFundNo,
    sModifyItem: '',
    sAuditTime: '',
    sAuditor: '',
    sAuditOpinion: '',
    sAuditStatus: ''
  };
  // 条目抽取列表页配置
  public queryPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };



  constructor(
    public contractService: ContractService,
    public dataService: DataService
  ) {

  }

  ngOnInit() {
    // 初始化日期控件
    flatpickr.localize(zh);
    // 字典值
    this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));
    // 产品类型
    this.sFundType = _.filter(this.dictionaryList.FUND_TYPE, function (item: any) {
      return item.sItemKey > 3 && item.sItemKey < 8;
    })
    // 赋值条目变更记录内码
    this.queryPost.nFundNo = this.contractBody.nFundNo;
    this.getSingleServiceInfo();
    this.getEntryChangeList();
  }
  // 查询-单个服务条目条目内容（根据FundNo）
  getSingleServiceInfo() {
    this.contractService.getSingleInfo(this.contractBody.nFundNo).subscribe(data => {
      if (data) {
        this.effectiveBody = data;
         console.log(this.effectiveBody);
      }
    })
  }
  // 查询条目变更记录
  getEntryChangeList(pageNum?, pageSize?) {
    let that = this;
    this.queryPost.sModifyItem = _.trim(this.queryPost.sModifyItem);
    this.queryPost.sAuditor = _.trim(this.queryPost.sAuditor);
    this.queryPost.sAuditOpinion = _.trim(this.queryPost.sAuditOpinion);
    this.queryPost.pageIndex = pageNum ? pageNum : this.queryPageInfo.currentPageNum;
    this.queryPost.pageSize = pageSize ? pageSize : this.queryPageInfo.pageSize;
    this.contractService.getEntryChangeList(this.queryPost).subscribe(data => {
      if (data) {
        this.queryList = data.list;
        this.queryPageInfo.currentPageNum = data.pageNum;
        this.queryPageInfo.totalPages = data.pages;
        this.queryPageInfo.total = data.total;
        this.queryPageInfo.startRow = data.startRow;
        this.queryPageInfo.endRow = data.endRow;
      }
    })
  }
  // 条目变更记录个数分页
  changepageSizeChange(pageSize: number) {
    if (pageSize !== this.queryPageInfo.pageSize) {
      this.queryPageInfo.pageSize = pageSize;
      this.getEntryChangeList();
    }
  }
  // 条目变更记录条数分页
  changePageNavigation(currentPageNum: number) {
    this.queryPageInfo.currentPageNum = currentPageNum;
    this.getEntryChangeList(this.queryPageInfo.currentPageNum, this.queryPageInfo.pageSize);
  }
  // 更新服务类条目基本信息(需要审核)
  editServiceInfo() {
   
    let that = this;
    // console.log("安徽沙哈达哈哈的撒和电话" + that.effectiveBody.sAuditOpinion)
    if (that.effectiveBody.sAuditOpinion == '' || that.effectiveBody.sAuditOpinion == undefined) {
      window['swal']("提示", "审核意见不能为空!", "warning");
    } else {
      window["swal"]({
        title: "注意",
        text: "是否确定保存条目",
        type: "warning",
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        showCancelButton: true,
        closeOnConfirm: false,
        closeOnCancel: true,
        showLoaderOnConfirm: true
      },
        function (isConfirm) {
          if (isConfirm) {
            // console.log(this.effectiveBody)
            that.contractService.edit(that.effectiveBody).subscribe(data => {
              if (data) {
                that.dataService.entryEmitter.emit('contractService');
                window['swal']("提示", "修改条目成功,请前往审核!", "success");
              }
            })
          }
        })
    }
  }
}
